home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / objects in c ƒ / Box.c next >
Encoding:
C/C++ Source or Header  |  1989-03-01  |  1.8 KB  |  108 lines  |  [TEXT/KAHL]

  1. /*
  2.  *            simple box class for the Objects-in-C tester
  3.  *
  4.  *            Copyright © John Wainwright 1988
  5.  *
  6.  */
  7.  
  8. #include "oic.h"
  9. #include "generics.h"
  10.  
  11. typedef struct                    /* box IV's                                    */
  12. {
  13.     object    tl;
  14.     object    br;
  15. } box_i;
  16. typedef object box;
  17.  
  18. class    Box;
  19.  
  20. externGeneric(offset, offsetGeneric)
  21. defGeneric(offset,         offsetGeneric,     "offset")
  22.  
  23. /* ------------------------------ Box Ops -------------------------------- */
  24.  
  25. static box
  26. _new(self, b, ap)
  27.     box        self;
  28.     box_i    *b;
  29.     struct
  30.     {
  31.         double    top;
  32.         double    left;
  33.         double    bottom;
  34.         double    right;
  35.     } *ap;
  36. {
  37.     extern class    Coord;
  38.     
  39.     b->tl = New(Coord, ap->left, ap->top);
  40.     b->br = New(Coord, ap->right, ap->bottom);
  41.     return Super(self);
  42. }
  43.  
  44. static box
  45. _offset(self, b, args)
  46.     box        self;
  47.     box_i    *b;
  48.     struct {double dx; int dy;} *args;
  49. {
  50.     offset(b->tl, args->dx, args->dy);
  51.     offset(b->br, args->dx, args->dy);
  52.     changed(self);
  53. }
  54.  
  55. static box
  56. _draw(self, b)
  57.     box        self;
  58.     box_i    *b;
  59. {
  60.     Rect     r;
  61.     
  62.     r.top = yCoord(b->tl);
  63.     r.left = xCoord(b->tl);
  64.     r.bottom = yCoord(b->br);
  65.     r.right = xCoord(b->br);
  66.  
  67.     FrameRect(&r);
  68. }
  69.  
  70. static
  71. _hasChanged(self, b, op)
  72.     box        self;
  73.     box_i    *b;
  74.     object    *op;
  75. {
  76.     gprintf(screen, "object : ");
  77.     print(*op);
  78.     gprintf(screen, "... has changed.\n");
  79.     
  80.     changed(self);            /* propogate changes, say */
  81. }
  82.  
  83. static string
  84. _replist(self, b)
  85.     box        self;
  86.     box_i     *b;
  87. {
  88.     register replist    replist;
  89.  
  90.     replist = New(Replist, "{", "}", ".");
  91.     add(replist, repList(b->tl), repList(b->br), END);
  92.     return replist;
  93. }
  94.  
  95. /*------------------------------ Init Classes --------------------------------*/
  96.  
  97. InitBoxClass()
  98. {
  99.     Box = NewClass(sizeof(box_i), 0, "Box", IndexMixin, DependentsMixin, END);
  100.     AddMethods(Box,
  101.         newGeneric,            _new,
  102.         drawGeneric,        _draw,
  103.         offsetGeneric,        _offset,
  104.         repListGeneric,        _replist,
  105.         hasChangedGeneric,    _hasChanged,
  106.         END);
  107. }
  108.